package com.ztc.ZtcRoute.service;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.service.AttachmentManager;
import com.ztc.ZtcClasses.service.ZtcClassesManager;
import com.ztc.ZtcClassesForSale.service.ZtcClassesForSaleManager;
import com.ztc.ZtcRoute.bean.ZtcRoute;
import com.ztc.ZtcRoute.dao.ZtcRouteDao;
import com.ztc.ZtcRouteAttachment.bean.ZtcRouteAttachment;
import com.ztc.ZtcRouteAttachment.dao.ZtcRouteAttachmentDao;
import com.ztc.ZtcRoutePrice.bean.ZtcRoutePrice;
import com.ztc.ZtcRoutePrice.dao.ZtcRoutePriceDao;
import com.ztc.ZtcRouteSite.bean.ZtcRouteSite;
import com.ztc.ZtcRouteSite.dao.ZtcRouteSiteDao;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ZtcRoute")
public class ZtcRouteManager {
	@Autowired
	ZtcRouteDao ztcRouteDao;
	@Autowired
	ZtcRoutePriceDao ztcRoutePriceDao;
	@Autowired
	ZtcRouteSiteDao ztcRouteSiteDao;
	@Autowired
	ZtcRouteAttachmentDao ztcRouteAttachmentDao;
	@Autowired
	AttachmentManager attachmentManager;
	@Autowired
	ZtcTransportManager ztcTransportManager;
	@Autowired
	ZtcClassesManager ztcClassesManager;
	@Autowired
	ZtcClassesForSaleManager ztcClassesForSaleManager;

	public String save(ZtcRoute ztcRoute) {
		return ztcRouteDao.save(ztcRoute);
	}

	public List<ZtcRoute> searchInfoEquals(ZtcRoute ztcRoute) {
		List<ZtcRoute> list = ztcRouteDao.searchInfoEquals(ztcRoute);
		return list;
	}
	public ZtcRoute getById(String Id){
		if(Id==null||Id.isEmpty()){return null;}
		else{return ztcRouteDao.get(Id);}
	}
	/**
	 * 根据线路，公司，页数，功能模块组合查询线路信息
	 * @param ztcRoute 线路对象
	 * @param transport 公司对象
	 * @param pageNumber 第几页
	 * @param moduleNumber 功能模块号：0.线路（待）审核1.线路信息
	 * @return 线路集合
	 */
	public List<ZtcRoute> searchInfoLikes(ZtcRoute ztcRoute, ZtcTransport transport, String pageNumber,
			String moduleNumber) {
		HashMap<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("ztcRoute", ztcRoute);
		arguments.put("pageNumber", pageNumber);
		int transportType = transport.getType();
		String statusSql = "";
		String orderBySql="";
		if (transportType == 0) {
			if ("0".equals(moduleNumber)) {
				statusSql = " where (ztc_route.status='1' or ztc_route.status='3')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time desc";
			} else {
				statusSql = " where (ztc_route.status='0' or ztc_route.status='2')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time asc";
			}
		} else if (transportType == 3) {
			if ("0".equals(moduleNumber)) {
				statusSql = " where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and (ztc_route.status='1' or ztc_route.status='3')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time desc";
			} else {
				statusSql = ", ztc_transport where ztc_route.fk_ztc_transport_id=ztc_transport.id and ztc_transport.parent_id='"+transport.getId()+"'"
						+ " and ztc_route.status='0'";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time asc";
			}
		} else if (transportType == 4) {
			if ("0".equals(moduleNumber)) {
				statusSql = " where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and (ztc_route.status='1' or ztc_route.status='3')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time desc";
			} else {
				statusSql = " where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and (ztc_route.status='0' or ztc_route.status='2')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time asc";
			}
		}
		arguments.put("statusSql", statusSql);
		arguments.put("orderBySql", orderBySql);
		List<ZtcRoute> list = ztcRouteDao.searchInfoLikes(arguments);
		return list;
	}

	/**
	 * 根据线路，公司，页数，功能模块组合统计线路信息
	 * @param ztcRoute 线路对象
	 * @param transport 公司对象
	 * @param moduleNumber 功能模块号：0.线路（待）审核1.线路信息
	 * @return 线路信息总条数
	 */
	public int count(ZtcRoute ztcRoute, ZtcTransport transport,String moduleNumber) {
		HashMap<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("ztcRoute", ztcRoute);
		int transportType = transport.getType();
		String statusSql = "";
		String orderBySql="";
		if (transportType == 0) {
			if ("0".equals(moduleNumber)) {
				statusSql = " where (ztc_route.status='1' or ztc_route.status='3')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time desc";
			} else {
				statusSql = " where (ztc_route.status='0' or ztc_route.status='2')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time asc";
			}
		} else if (transportType == 3) {
			if ("0".equals(moduleNumber)) {
				statusSql = " where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and (ztc_route.status='1' or ztc_route.status='3')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time desc";
			} else {
				statusSql = ", ztc_transport where ztc_route.fk_ztc_transport_id=ztc_transport.id and ztc_transport.parent_id='"+transport.getId()+"'"
						+ " and ztc_route.status='0'";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time asc";
			}
		} else if (transportType == 4) {
			if ("0".equals(moduleNumber)) {
				statusSql = " where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and (ztc_route.status='1' or ztc_route.status='3')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time desc";
			} else {
				statusSql = " where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and (ztc_route.status='0' or ztc_route.status='2')";
				orderBySql=" order by ztc_route.status asc,ztc_route.add_time asc";
			}
		}
		arguments.put("statusSql", statusSql);
		arguments.put("orderBySql", orderBySql);
		return ztcRouteDao.getCount(arguments);
	}

	/**
	 * 根据线路，公司，页数查询用于发布的线路信息
	 * @param ztcRoute 线路对象
	 * @param transport 公司对象
	 * @param pageNumber 第几页
	 * @return 线路信息集合
	 */
	public List<ZtcRoute> searchInfoLikesForClassesPublish(ZtcRoute ztcRoute, ZtcTransport transport, String pageNumber) {
		HashMap<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("ztcRoute", ztcRoute);
		arguments.put("pageNumber", pageNumber);
		int transportType = transport.getType();
		String statusSql = "";
		String orderBySql="";
		if (transportType == 0) {
			statusSql = " where ztc_route.status='1'";
		} else {
			statusSql =" where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and ztc_route.status='1'";
		}
		orderBySql=" order by ztc_route.add_time asc";
		arguments.put("statusSql", statusSql);
		arguments.put("orderBySql", orderBySql);
		List<ZtcRoute> list = ztcRouteDao.searchInfoLikesForClassesPublish(arguments);
		return list;
	}

	/**
	 * 根据线路，公司统计用于发布的线路信息
	 * @param ztcRoute 线路对象
	 * @param transport 公司对象
	 * @return 线路信息总条数
	 */
	public int countForPublish(ZtcRoute ztcRoute, ZtcTransport transport) {
		HashMap<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("ztcRoute", ztcRoute);
		int transportType = transport.getType();
		String statusSql = "";
		String orderBySql="";
		if (transportType == 0) {
			statusSql = " where ztc_route.status='1'";
		} else {
			statusSql =" where ztc_route.fk_ztc_transport_id='"+transport.getId()+"' and ztc_route.status='1'";
		}
		orderBySql=" order by ztc_route.add_time asc";
		arguments.put("statusSql", statusSql);
		arguments.put("orderBySql", orderBySql);
		return ztcRouteDao.getCountForClassesPublish(arguments);
	}

	/**
	 * 获取线路的所有站点
	 * @param ztcRoute 线路对象
	 * @return 线路站点集合
	 */
	public List<ZtcRouteSite> getSites(ZtcRoute ztcRoute) {
		ZtcRouteSite ztcRouteSite = new ZtcRouteSite();
		ztcRouteSite.setFkZtcRouteId(ztcRoute.getId());
		return ztcRouteSiteDao.searchInfoEquals(ztcRouteSite);
	}

	/**
	 * 获取线路的所有价格
	 * @param ztcRoute 线路对象
	 * @return 线路价格集合
	 */
	public List<ZtcRoutePrice> getPrices(ZtcRoute ztcRoute) {
		ZtcRoutePrice ztcRoutePrice = new ZtcRoutePrice();
		ztcRoutePrice.setFkZtcRouteId(ztcRoute.getId());
		return ztcRoutePriceDao.searchInfoEquals(ztcRoutePrice);
	}

	/**
	 * 获取线路的所有附件
	 * @param ztcRoute 线路对象
	 * @return 线路附件集合
	 */
	public List<ZtcRouteAttachment> getAttachments(ZtcRoute ztcRoute) {
		ZtcRouteAttachment ztcRouteAttachment = new ZtcRouteAttachment();
		ztcRouteAttachment.setFkZtcRouteId(ztcRoute.getId());
		List<ZtcRouteAttachment> ztcRouteAttachments = ztcRouteAttachmentDao.searchInfoEquals(ztcRouteAttachment);
		if (ztcRouteAttachments != null && ztcRouteAttachments.size() > 0) {
			for (int i = 0; i < ztcRouteAttachments.size(); i++) {
				ZtcRouteAttachment routeAttachment = ztcRouteAttachments.get(i);
				if (!"".equals(routeAttachment.getFkAttachmentId()) && routeAttachment.getFkAttachmentId() != null) {
					Attachment attachment = attachmentManager.get(routeAttachment.getFkAttachmentId());
					routeAttachment.setAttachment(attachment);
				}
			}
		}
		return ztcRouteAttachments;
	}

	/**
	 * 根据线路id或公司id删除附件
	 * @param id 线路id
	 * @param transportId 公司id
	 * @return 操作结果信息
	 */
	public String delete(String id, String transportId) {
		return ztcRouteDao.delete(id, transportId);
	}

	public String update(ZtcRoute ztcRoute) {
		return ztcRouteDao.update(ztcRoute);
	}

	/**
	 * 查询所有的过期线路并且设置为过期
	 * @return：0.操作失败1.操作成功
	 */
	public String examineOverdue(){
		List<ZtcRoute> routeList=ztcRouteDao.searchOverdueRoute();
		for(int i=0;i<routeList.size();i++){
			ZtcRoute route =routeList.get(i);
			route.setStatus(0);
			String routeId = ztcRouteDao.update(route);
			if ("0".equals(routeId)) {
				return "0";
			}
		}
		return "1";
	}

    public List<Map> getRouteListMapByTid(String transportId, Integer type) {
		return ztcRouteDao.getRouteListMapByTid(transportId,type);
    }
}
